// LoginWindow.java
package org.zjx.core.impl;

import com.sun.jna.platform.win32.WinDef;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.zjx.command.ICommand;
import org.zjx.command.StrategyCommand;
import org.zjx.core.AbstractWindow;
import org.zjx.manager.WindowManager;
import org.zjx.state.StateManager;
import org.zjx.util.HwndUtil;

import java.awt.*;
import java.util.concurrent.CompletableFuture;

/**
 * 登录窗口 - 添加自动转换逻辑
 */
@Slf4j
@Getter
public class LoginWindow extends AbstractWindow {
    private final StateManager stateManager;

    public LoginWindow(WinDef.HWND hwnd) {
        super(hwnd);
        stateManager = new StateManager();
        submitCommand(new StrategyCommand(this,stateManager));
    }

    @Override
    public Rectangle getTrueBounds() {
        return new Rectangle(8,31,800,600);
    }

    @Override
    public String getId() {
        return "LoginWindow-" + getHwnd().hashCode();
    }

    /**
     * 提交命令，在命令执行后检查是否需要转换窗口类型
     */
    @Override
    public CompletableFuture<Boolean> submitCommand(ICommand command) {
        return super.submitCommand(command)
                .thenApply(success -> {
                    if (success) {
                        // 命令执行成功后检查窗口状态
                        checkAndConvertWindow();
                    }
                    return success;
                });
    }

    @Override
    public void executeCommand(ICommand command) {
        super.executeCommand(command);

        // 命令执行成功后检查队列状态
        checkAndSubmitStrategyCommand();
    }

    /**
     * 检查并转换窗口类型
     */
    public void checkAndConvertWindow() {
        try {
            // 检查窗口标题是否发生变化（登录完成）
            String currentTitle = HwndUtil.getWindowTitle(getHwnd());
            if (currentTitle != null && currentTitle.contains("[")) {
                // 窗口标题包含"[", 说明登录完成，转换为GameWindow
                log.info("[LoginWindow] 检测到登录完成，准备转换窗口类型: {}", getId());

                // 获取WindowManager实例并转换窗口
                WindowManager windowManager = WindowManager.getInstance();
                windowManager.convertToGameWindow(this);
            }
        } catch (Exception e) {
            log.error("[LoginWindow] 检查窗口转换失败: {}", getId(), e);
        }
    }

    /**
     * 检查并提交策略命令
     */
    public void checkAndSubmitStrategyCommand() {
        try {
            // 检查命令队列是否为空
            if (getCommandQueue().isEmpty()) {
                // 队列为空，提交策略命令
                log.info("[GameWindow] 检测到命令队列为空，准备提交策略命令: {}", getId());

                StrategyCommand strategyCommand = new StrategyCommand(this, stateManager);
                submitCommand(strategyCommand);
                log.debug("[GameWindow] 策略命令已提交: {}", getId());
            }
        } catch (Exception e) {
            log.error("[GameWindow] 检查并提交策略命令失败: {}", getId(), e);
        }
    }

    /**
     * 手动触发窗口转换（供外部调用）
     */
    public void convertToGameWindow() {
        try {
            WindowManager windowManager = WindowManager.getInstance();
            windowManager.convertToGameWindow(this);
        } catch (Exception e) {
            log.error("[LoginWindow] 手动转换窗口失败: {}", getId(), e);
        }
    }

    /**
     * 执行登录逻辑，登录完成后自动转换
     */
    public void performLogin() {
        // 这里实现具体的登录逻辑
        // 比如输入账号密码、点击登录按钮等

        // 登录完成后检查转换
        checkAndConvertWindow();
    }
}